General: Introduce wp_get_branch_version() helper to extract major.minor version safely.#11211
General: Introduce wp_get_branch_version() helper to extract major.minor version safely.#11211tejas0306 wants to merge 4 commits intoWordPress:trunkfrom
Conversation
|
Hi @tejas0306! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
src/wp-includes/functions.php
Outdated
| } | ||
|
|
||
| if ( 'minor' === $part ) { |
There was a problem hiding this comment.
| } | |
| if ( 'minor' === $part ) { | |
| } elseif ( 'minor' === $part ) { |
| * current WordPress version from wp_get_wp_version(). | ||
| * @return string The major version string in "x.y" format (e.g. "7.0"). | ||
| */ | ||
| function wp_get_branch_version( $version = '' ) { |
There was a problem hiding this comment.
I don't think this is required, the need in core isn't wide enough for what is relatively simple code.
There was a problem hiding this comment.
The problem is that it's needed in about half a dozen different places in core. Currently these are using a mixture of different ad-hoc methods to find the version number, and some of these are actually wrong/buggy.
There was a problem hiding this comment.
Thanks for the review.
The intention of wp_get_branch_version() here is mainly to consolidate several ad-hoc approaches currently used in core to derive the branch version.
At the moment, different files use different methods such as (float) casting, substr( $ver, 0, 3 ), or preg_split() / explode() logic. Some of these are fragile (for example substr( $ver, 0, 3 ) will break for versions >= 10, and (float) casting can produce incorrect results for x.0 branches).
This PR updates the current call sites that need the branch version to use a shared helper instead:
wp-admin/admin-header.phpwp-admin/includes/class-core-upgrader.phpwp-admin/includes/class-wp-site-health-auto-updates.phpwp-admin/includes/plugin-install.phpwp-admin/includes/theme.php
The goal is mainly to keep the behavior consistent and avoid repeating slightly different parsing logic across these files.
Happy to adjust the implementation or limit the scope if a smaller change would be preferred.
There was a problem hiding this comment.
@tejas0306 something went wrong last time trunk was merged so the PR is showing 95 changed files instead of the few that have actually changed. Are you able to remerge / rebase so I can follow this up?
…nor version safely.
This introduces a new helper function `wp_get_branch_version()` for extracting the WordPress branch version (major.minor) from a version string.
Currently, several different approaches are used across core to determine the branch version, including:
- `(float)` casting of `get_bloginfo( 'version' )`
- `substr( $ver, 0, 3 )`
- `explode( '.' )` based parsing
- `preg_split()` in `class-core-upgrader.php`
Some of these approaches are fragile or incorrect:
- `(float)` casting can produce incorrect values due to floating-point precision issues.
- `substr( $ver, 0, 3 )` breaks for versions >= 10.
- Multiple inconsistent approaches make maintenance harder.
This patch introduces `wp_get_branch_version()` using a string-based approach:
```php
function wp_get_branch_version( $version = '' ) {
if ( '' === $version ) {
$version = wp_get_wp_version();
}
$parts = preg_split( '/[.-]/', $version, 3 );
return $parts[0] . '.' . ( $parts[1] ?? '0' );
}
… edge case, add tests.
…p_version_minor()
60629bf to
d584d96
Compare
This introduces a new helper function
wp_get_branch_version()for extracting the WordPress branch version (major.minor) from a version string.Currently, several different approaches are used across core to determine the branch version, including:
(float)casting ofget_bloginfo( 'version' )substr( $ver, 0, 3 )explode( '.' )based parsingpreg_split()inclass-core-upgrader.phpSome of these approaches are fragile or incorrect:
(float)casting can produce incorrect values due to floating-point precision issues.substr( $ver, 0, 3 )breaks for versions >= 10.This patch introduces
wp_get_branch_version()using a string-based approach:Trac ticket: https://core.trac.wordpress.org/ticket/64830